package com.fish.common.strategy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 文件上传策略工厂
 */
@Slf4j
@Component
public class FileUploadStrategyFactory {
    
    /**
     * 策略容器
     */
    private final Map<String, FileUploadStrategy> strategyMap = new ConcurrentHashMap<>();
    
    /**
     * 默认策略类型
     */
    private static final String DEFAULT_STRATEGY = "minio";
    
    /**
     * 通过构造器注入所有策略实现
     */
    @Autowired
    public FileUploadStrategyFactory(List<FileUploadStrategy> strategies) {
        for (FileUploadStrategy strategy : strategies) {
            strategyMap.put(strategy.getType(), strategy);
            log.info("注册文件上传策略: {}", strategy.getType());
        }
    }
    
    /**
     * 获取指定类型的策略
     * 
     * @param type 策略类型
     * @return 文件上传策略
     */
    public FileUploadStrategy getStrategy(String type) {
        FileUploadStrategy strategy = strategyMap.get(type);
        if (strategy == null) {
            log.warn("未找到策略类型: {}, 使用默认策略: {}", type, DEFAULT_STRATEGY);
            return strategyMap.get(DEFAULT_STRATEGY);
        }
        return strategy;
    }
    
    /**
     * 获取默认策略
     * 
     * @return 默认文件上传策略
     */
    public FileUploadStrategy getDefaultStrategy() {
        return getStrategy(DEFAULT_STRATEGY);
    }
}
